import _ from 'lodash'
import {createRouter, createWebHashHistory, RouteRecordRaw} from "vue-router"
import {setupStudioRouter} from '@/index'

export const Layout = () => import("./layout.vue");

/**
 * 利用vite扫描所有的页面组件 | /src/pages/Demo.vue -> import('/src/pages/Demo.vue') -> Module.default
 */
const pages = import.meta.glob(
    ['./pages/**/*.vue'],
    {eager: false},
);
window['pages'] = pages

/**
 * 从 widgets 文件夹中分析出所有的 widget
 */
const widgetModule = import.meta.globEager('./widgets/**/*.js')
const widgets = {}
_.forOwn(widgetModule, (value: any, key: string) => {
    // key '/src/widgets/AreaSearch.js' => AreaSearch
    if (_.startsWith(key, '/src/widgets/')) {
        const name = key.substring(13, key.length - 3)
        widgets[name] = value.default
        widgets[name]._filePath = key
    }
})
window['widgets'] = widgets

/**
 * 允许启用 OneCodeStudio
 * 从所有页面分析出所有 designJS文件
 */
const designJS = {};

const designJsModule = import.meta.glob('./pages/**/*.design.js', {eager: true})
_.forOwn(designJsModule, (value: any, key: string) => {
    if (key.startsWith("./pages")) {
        key = key.substring(8);
    }
    designJS[key] = async () => {
        return await value();
    };
})
window['designJS'] = designJS

/**
 * 从 ui-mobile 文件夹中分析出所有的移动端页面
 */
const mobilePages = import.meta.globEager('./pages_mobile/**/*.vue')
window['pages_mobile'] = mobilePages

/**
 * 静态路由表定义
 */
export const constantRoutes: RouteRecordRaw[] = [
    {
        path: "/redirect",
        component: Layout,
        meta: {hidden: true},
        children: [
            {
                path: "/redirect/:path(.*)",
                component: () => import("./pages/redirect.vue"),
            },
        ],
    },

    {
        path: "/login",
        component: () => import("./pages/login.vue"),
        meta: {hidden: true},
    },

    {
        path: "/",
        component: Layout,
        redirect: "/dashboard",
        children: [
            {
                path: "dashboard",
                component: () => import("./pages/dashboard.vue"),
                name: "Dashboard",
                meta: {title: "dashboard", icon: "homepage", affix: true},
            },
            {
                path: "401",
                component: () => import("./pages/error-page/401.vue"),
                meta: {hidden: true},
            },
            {
                path: "404",
                component: () => import("./pages/error-page/404.vue"),
                meta: {hidden: true},
            },
            {
                path: 'Test',
                component: () => import("./pages/Test.vue"),
                props: {_input: {}},
                meta: {hidden: true},
            },
            {
                path: 'inbound',
                children: [
                    {
                        path: 'asn_in',
                        children: [
                            {
                                path: 'AsnIn',
                                component: () => import("./pages/inbound/asn_in/AsnIn.vue"),
                                props: {_input: {}},
                                meta: {hidden: true},
                            }
                        ]
                    }
                ]
            }
        ],
    },
];

// 安装 studio 的路由
setupStudioRouter(constantRoutes, '/one-code-studio/')

/**
 * 创建路由器
 */
const router = createRouter({
    history: createWebHashHistory(),
    routes: constantRoutes as RouteRecordRaw[],
    // 刷新时，滚动条位置还原
    scrollBehavior: () => ({left: 0, top: 0}),
});

/**
 * 重置路由
 */
export function resetRouter() {
    router.replace({path: "/login"});
}

/**
 * 获取路由数据
 */
async function getRoutes(): Promise<RouteRecordRaw[]> {
    return [];
}

export default router;